/**
 * 
 */
package org.vsg.common.pagination;

import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author vison_ruan
 *
 */
public abstract class AbstractPagination implements Pagination {

	private final static Logger logger = LoggerFactory.getLogger(AbstractPagination.class);

	
	protected List dataCollection;
	
	protected int totalRecord;
	
	protected int pageSize;
	
	protected int totalPages;
	
	protected int pageIndex; // set the default current page is '0'
	
	protected final int FIRSTPAGEINDEX = 0;
	
	protected int LASTPAGEINDEX;
	
	//protected String expMessage_pageSize = "Page size does not set it less than '1'.Please redefine it.";
	protected String expMessage_pageSize = "ÿ����Ŀ��������'1'���������趨!";
	
	//protected String expMessage_gotoPageOverTotalPages = "The page index isn't over the maximal pages! ";
	protected String expMessage_gotoPageOverTotalPages = "��ǰҳ���趨�������ҳ����! ";

	//protected String expMessage_gotoPageUnderTotalPages = "The page index isn't less than zero";
	protected String expMessage_gotoPageUnderTotalPages = "��ǰҳ���趨������Сҳ����!";
	
	
	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#getPageIndex()
	 */
	public int getPageIndex() {
		// TODO Auto-generated method stub
		return pageIndex;
	}
	

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#getPageSize()
	 */
	public int getPageSize() {
		// TODO Auto-generated method stub
		return pageSize;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#gotoPage(int)
	 */
	public void setPageIndex(int pageNumber) throws PaginationException {
		// TODO Auto-generated method stub
		pageIndex = pageNumber;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#isFirstPage()
	 */
	public boolean isFirstPage() {
		// TODO Auto-generated method stub
		boolean isFirstPage = false;
		if (this.dataCollection.isEmpty()) {
			isFirstPage = true;
		} else {
			if (this.pageIndex == FIRSTPAGEINDEX) { // get the position
				isFirstPage = true;
			}
		}
		return isFirstPage;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#isLastPage()
	 */
	public boolean isLastPage() {
		// TODO Auto-generated method stub
		boolean isLastPage = false;
		if (this.dataCollection.isEmpty()) {
			isLastPage = true;
		} else {
			// get the position
			if (this.pageIndex == LASTPAGEINDEX) {
				isLastPage = true;;
			}
		}
		return isLastPage;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#isNextPageAvailable()
	 */
	public boolean isNextPageAvailable() {
		// TODO Auto-generated method stub
		boolean  available = false;
		
		int afterCurrentPage = pageIndex + 1;
		if (!(afterCurrentPage > LASTPAGEINDEX)) {
			available = true;
		} 
		
		return available;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#isPreviousPageAvailable()
	 */
	public boolean isPreviousPageAvailable() {
		// TODO Auto-generated method stub
		boolean  available = false;
		int beforeCurrentPage = pageIndex - 1;
		if (! (beforeCurrentPage < FIRSTPAGEINDEX)) {
			available = true;
		}
		return available;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#getPaginationList()
	 */
	public Collection getPaginationList() {
		// TODO Auto-generated method stub
		return dataCollection;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#getTotalRecord()
	 */
	public int getTotalRecord() {
		// TODO Auto-generated method stub
		return totalRecord;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#getTotalPages()
	 */
	public int getTotalPages() {
		// TODO Auto-generated method stub
		return this.totalPages;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#getFirstPageIndex()
	 */
	public int getFirstPageIndex() {
		// TODO Auto-generated method stub
		return FIRSTPAGEINDEX;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.Pagination#getLastPageIndex()
	 */
	public int getLastPageIndex() {
		// TODO Auto-generated method stub
		return LASTPAGEINDEX;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public void setDataList(List dataList) {
		this.dataCollection = dataList;
	}
	
	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}
	
	/**
	 * init prop set
	 *
	 */
	public abstract void initAfterPropSet() throws PaginationException; 

}
